package tools;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.util.Log;


import model.LocationModel;

public class LocationParser {

	private final static String TAG = "LocationParser";

	// Constructor
	public LocationParser() {

	}

	/**
	 * Getting XML from URL making HTTP request
	 * 
	 * @param url
	 *            string
	 * @return Location model
	 */
	public LocationModel getLocation(String stringUrl) {
		URL url;

		LocationModel loc = new LocationModel();

		try {
			url = new URL(stringUrl);

			URLConnection connection;
			connection = url.openConnection();

			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			int responseCode = httpConnection.getResponseCode();

			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream in = httpConnection.getInputStream();

				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();

				// Parse the location feed.
				Document dom = db.parse(in);
				Element docEle = dom.getDocumentElement();

				// Get a city entry.
				NodeList nl = docEle.getElementsByTagName("CoordLocation");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {
						Node node = nl.item(i);

						if (node.hasAttributes()) {
							Attr attr = (Attr) node.getAttributes()
									.getNamedItem("name");
							if (attr != null) {
								loc.setLocationName(attr.getValue());
							}
							attr = (Attr) node.getAttributes()
									.getNamedItem("x");
							if (attr != null) {
								loc.setX(Integer.parseInt(attr.getValue()));
							}
							attr = (Attr) node.getAttributes()
									.getNamedItem("y");
							if (attr != null) {
								loc.setY(Integer.parseInt(attr.getValue()));
							}
							attr = (Attr) node.getAttributes().getNamedItem(
									"type");
							if (attr != null) {
								loc.setType(attr.getValue());
							}
						}
					}
				}

				nl = docEle.getElementsByTagName("StopLocation");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < 1; i++) {
						Node node = nl.item(i);

						if (node.hasAttributes()) {
							Attr attr = (Attr) node.getAttributes()
									.getNamedItem("name");
							if (attr != null) {
								loc.setLocationName(attr.getValue());
							}
							attr = (Attr) node.getAttributes()
									.getNamedItem("x");
							if (attr != null) {
								loc.setX(Integer.parseInt(attr.getValue()));
							}
							attr = (Attr) node.getAttributes()
									.getNamedItem("y");
							if (attr != null) {
								loc.setY(Integer.parseInt(attr.getValue()));
							}
							attr = (Attr) node.getAttributes().getNamedItem(
									"id");
							if (attr != null) {
								loc.setId(Integer.parseInt(attr.getValue()));
							}
							attr = (Attr) node.getAttributes().getNamedItem(
									"type");

						}
					}
				}

			}
		} catch (MalformedURLException e) {
			Log.d(TAG, "MalformedURLException");
		} catch (IOException e) {
			Log.d(TAG, "IOException");
		} catch (ParserConfigurationException e) {
			Log.d(TAG, "ParserConfigurationException");
		} catch (SAXException e) {
			Log.d(TAG, "SAXException");
		} finally {
		}
		return loc;
	}

	public ArrayList<String> getLocationStringArray(String stringUrl) {

		ArrayList<String> locListString = new ArrayList<String>();
		
		try {

			URL url = new URL(stringUrl);

			URLConnection connection;
			connection = url.openConnection();

			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			int responseCode = httpConnection.getResponseCode();

			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream in = httpConnection.getInputStream();

				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();

				// Parse the location feed.
				Document dom = db.parse(in);
				Element docEle = dom.getDocumentElement();

				// Get a city entry.

				NodeList nl = docEle.getElementsByTagName("StopLocation");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < nl.getLength(); i++) {

						Node node = nl.item(i);

						if (node.hasAttributes()) {

							Attr attr = (Attr) node.getAttributes()
									.getNamedItem("name");
							if (attr != null) {
								locListString.add(attr.getValue());
							}
						}
					}
				}

				nl = docEle.getElementsByTagName("CoordLocation");
				if (nl != null && nl.getLength() > 0) {
					for (int i = 0; i < nl.getLength(); i++) {

						Node node = nl.item(i);

						if (node.hasAttributes()) {

							Attr attr = (Attr) node.getAttributes()
									.getNamedItem("name");

							if (attr != null) {
								locListString.add(attr.getValue());
							}

						}
					}
				}

			}
		} catch (MalformedURLException e) {
			Log.d(TAG, "MalformedURLException");
		} catch (IOException e) {
			Log.d(TAG, "IOException");
		} catch (ParserConfigurationException e) {
			Log.d(TAG, "ParserConfigurationException");
		} catch (SAXException e) {
			Log.d(TAG, "SAXException");
		} finally {
		}
		
		return locListString;
	}

}
